﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace community
{
    public partial class hypertensionreport : Form
    {

        public static string hy_JMBH = "";//双击datagridview1的记录的居民编号
        public static string hy_JBMC = "";//双击datagridview1的记录的疾病名称
        string s;
        public static DateTime hy_SFSJ;//双击datagridview1的记录的随访时间
        SqlConnection cn;
        SqlDataAdapter dt;
        DataSet ds;
        string sql = "select a.inhabitantcode_chr as 居民编号,a.residentname_chr as 居民姓名,a.communityname_chr as 所属社区,a.sex_chr as 居民性别,a.birth_dat as 出生日期,b.diseasename_vchr  as 疾病名称,b.happendate_dat as 患病时间,c.RECORDDATE_DAT as 随访时间,c.NEXTRECORDATE_DAT as 下次随访时间,c.RECORDDOCNAME_VCHR as 随访医生 from T_CS_INHABITANTRECORD_NEW as a,t_cs_membermedical as b,T_CS_HYPERTENSIONVISITRECORD as c  where c.inhabitantcode_chr=a.inhabitantcode_chr and a.inhabitantcode_chr=b.inhabitantcode_chr and c.RECORDDATE_DAT!='' and b.diseasename_vchr like '%高血压%' ";
        
        public hypertensionreport()
        {
            InitializeComponent();
        }

        private void hypertensionreport_Load(object sender, EventArgs e)
        {
            hr(sql);
        }
        private void hr(string str)
        {
            cn = new SqlConnection(SQL.conStr);
            dt = new SqlDataAdapter(str, cn);
            ds = new DataSet();
            cn.Open();
            dt.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
            cn.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            sql = "select a.inhabitantcode_chr as 居民编号,a.residentname_chr as 居民姓名,a.communityname_chr as 所属社区,a.sex_chr as 居民性别,a.birth_dat as 出生日期,b.diseasename_vchr  as 疾病名称,b.happendate_dat as 患病时间,c.RECORDDATE_DAT as 随访时间,c.NEXTRECORDATE_DAT as 下次随访时间, c.RECORDDOCNAME_VCHR as 随访医生 from T_CS_INHABITANTRECORD_NEW as a,t_cs_membermedical as b,T_CS_HYPERTENSIONVISITRECORD as c  where c.inhabitantcode_chr=a.inhabitantcode_chr and a.inhabitantcode_chr=b.inhabitantcode_chr and c.RECORDDATE_DAT!='' and b.diseasename_vchr like '%高血压%' ";
            if ((checkBox2.Checked == true) && (textBox1.Text == ""))
            {
                MessageBox.Show("请输入检索的随访医生", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if ((checkBox4.Checked == true) && (textBox2.Text == ""))
            {
                MessageBox.Show("请输入检索的社区", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if ((checkBox1.Checked == true) && (dateTimePicker1.Text == "") || (dateTimePicker2.Text == ""))
            {
                MessageBox.Show("请输入随访时间的时间段", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if ((checkBox3.Checked == true) && (dateTimePicker3.Text == "") || (dateTimePicker4.Text == ""))
            {
                MessageBox.Show("请输入下次随访时间的时间段", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            if (checkBox2.Checked == true && textBox1.Text != "")
                sql += "  and c.RECORDDOCNAME_VCHR='" + textBox1.Text.Trim() + "'";
            if (checkBox4.Checked == true && textBox2.Text != "")
                sql += "  and a.communityname_chr='" + textBox2.Text.Trim() + "'";
            if (checkBox1.Checked == true && dateTimePicker1.Text != "" && dateTimePicker2.Text != "")
                sql += "  and (c.RECORDDATE_DAT between '" + Convert.ToDateTime(dateTimePicker1.Text) + "' and '" + Convert.ToDateTime(dateTimePicker2.Text) + "')";
            if (checkBox2.Checked == true && dateTimePicker3.Text != "" && dateTimePicker4.Text != "")
                sql += "  and (c.NEXTRECORDATE_DAT between '" + Convert.ToDateTime(dateTimePicker3.Text) + "'  and '" + Convert.ToDateTime(dateTimePicker4.Text) + "')";
            hr(sql);

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
            }
            else
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked == true)
            {
                dateTimePicker3.Enabled = true;
                dateTimePicker4.Enabled = true;
            }
            else
            {
                dateTimePicker3.Enabled = false;
                dateTimePicker4.Enabled = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                textBox1.Enabled = true;
            }
            else
            {
                textBox1.Enabled = false;
                textBox1.Text = "";
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked == true)
            {
                textBox2.Enabled = true;
            }
            else
            {
                textBox2.Enabled = false;
                textBox2.Text = "";
            }
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            dataGridView3.Visible = true;
            cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sqlStr = "select EMPID_CHR as 职工工号,LASTNAME_VCHR as 职工姓名  from T_BSE_EMPLOYEE ";
            dt = new SqlDataAdapter(sqlStr, cn);
            ds = new DataSet();
            dt.Fill(ds);
            this.dataGridView3.DataSource = ds.Tables[0];
            cn.Close();
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            dataGridView2.Visible = true;
            cn = new SqlConnection(SQL.conStr);
            cn.Open();
            string sqlStr = "select COMMUNITYCODE_CHR as 社区编号,COMMUNITYNAME_VCHR as 社区名称  from t_cs_community  where  CURRENTCS_INT=1";
            dt = new SqlDataAdapter(sqlStr, cn);
            ds = new DataSet();
            dt.Fill(ds);
            this.dataGridView2.DataSource = ds.Tables[0];
            cn.Close();
        }

        private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textBox1.Text = dataGridView3.Rows[e.RowIndex].Cells[1].Value.ToString();
                if (textBox1.Text != null) dataGridView3.Visible = false;
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                textBox2.Text = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
                if (textBox1.Text != null) dataGridView2.Visible = false;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                hy_JBMC = "";
                hy_JMBH = "";
                hy_SFSJ = Convert.ToDateTime(s);
                hy_JMBH = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
                hy_JBMC = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                string sfsj = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
                hy_SFSJ = Convert.ToDateTime(sfsj);
            }
            this.Close();
        }

         
    }
}
